﻿using System;

namespace OpenWaves.EPiServer.Localization
{
    public class TranslationEntry : ITranslationEntry, IEquatable<TranslationEntry>
    {
        private readonly TranslationKeyCollection parent;
        private readonly string name;

        public TranslationEntry(string name, TranslationKeyCollection parent)
        {

            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("Name cannot be empty", "name");
            }

            this.parent = parent;
            this.name = name;
        }

        public string Name
        {
            get { return this.name; }
        }

        public virtual string Path
        {
            get
            {
                return this.parent == null
                           ? "/" + this.name
                           : string.Format("{0}/{1}", this.parent.Path, this.name);


            }
        }


        public bool Equals(TranslationEntry other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Equals(other.parent, this.parent) && Equals(other.name, this.name);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof(TranslationEntry)) return false;
            return this.Equals((TranslationEntry)obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return ((this.parent != null ? this.parent.GetHashCode() : 0) * 397) ^ (this.name != null ? this.name.GetHashCode() : 0);
            }
        }

        public static bool operator ==(TranslationEntry left, TranslationEntry right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(TranslationEntry left, TranslationEntry right)
        {
            return !Equals(left, right);
        }

        public override string ToString()
        {
            return string.Format("Path: {0}", this.Path);
        }
    }
}
